#include <iostream>
using namespace std;
#include <cstring>
#define BUFFER_SIZE 10000000;

/*
    抽象类:
        1，具有纯虚函数的类叫抽象类。纯虚函数:虚函数初始化为0.
        2，抽象类不能实例化对象
*/
class Exception
{
public:
    virtual string what() const = 0; 
};

class DivideException : public Exception
{
public:
    string what() const
    {
        cout << "不能除以0" << endl;
        return "不能除以0";
    }
};

class MemoryFlowException : public Exception
{
public:
    string what() const
    {
        cout << "内存溢出" << endl;
        return "内存溢出";
    }
};

int divide(int val1, int val2) noexcept(false)
{
    if(val2 == 0)
    {
        throw DivideException();
    }
    return val1 / val2;
}

int simulateNewException()
{
    for(int idx = 0; idx < 1000; idx++)
    {
        try
        {
            int * num = new int [1000000];
        }
        catch(const std::exception& e)
        {
            cout << "内存溢出" << e.what() << endl;
            break;
        }
        
    }
}

class Person
{
public:
    Person()
    {
        cout << "Person()" << endl;
    }
public:
    int m_age;
};

class Student : public Person
{
public:
    Student()
    {
        cout << "Student()" << endl;
    }  
public:
    int m_score;
};

int simulateDynamicCastExceptio()
{
    Person * p1 = new Person();/* 自己指自己*/
    Person * p2 = new Student();/* 父类指向子类对象*/

    try
    {
        Student *stu1 = dynamic_cast<Student *>(p1);
        cout << "Stu1" << stu1 <<endl;
    }
    catch(const std::bad_cast &e)
    {
        cout << "dyanmic_catch" << e.what() << endl;
    }

}

int main()
{
    try
    {
        /* code */
        int val1 = 100;
        int val2 = 0;
        int ret = divide(val1, val2);
        cout << "ret" << ret <<endl;
    }
    catch(const Exception& e)
    {
        cout << e.what() << endl;
    }
    

    cout << "hello world" << endl;
    return 0;
}